package com.tpv.tv.lib.pdf;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.widget.ProgressBar;

import androidx.core.content.ContextCompat;

public class PdfProgressBar extends ProgressBar {

    private Paint paint;
    private int backgroundColor;
    private RectF backgroundRect;
    private static final float backgroundRectPadding = 15f;

    public PdfProgressBar(Context context, AttributeSet attrs) {
        super(new ContextThemeWrapper(context, android.R.style.Widget_Material_Light_ProgressBar), attrs);
        initView();
    }

    public PdfProgressBar(Context context) {
        super(new ContextThemeWrapper(context, android.R.style.Widget_Material_Light_ProgressBar));
        initView();
    }

    private void initView() {
        paint = new Paint();
        backgroundColor = ContextCompat.getColor(getContext(), R.color.page_indicator);
        CircularProgressDrawable drawable = new CircularProgressDrawable(1, getContext());
//        drawable.setTint(ContextCompat.getColor(getContext(), android.R.color.white));
        drawable.setShowBackground(false);
        setIndeterminateDrawable(drawable);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // 获取控件的宽度和高度
        int width = getWidth();
        int height = getHeight();

        // 绘制背景圆角矩形（进度条背景）
        paint.setColor(backgroundColor);
        if(backgroundRect == null) {
            backgroundRect = new RectF(0, 0,
                    width, height);
        }
        // 绘制圆角边框
        canvas.drawRoundRect(backgroundRect, backgroundRectPadding, backgroundRectPadding, paint);
        super.onDraw(canvas);
    }
}
